//
//  RTJChangeBankView.m
//  RuiTaiJinProject
//
//  Created by jayson on 2018/12/29.
//  Copyright © 2018年 leewei. All rights reserved.
//

#import "RTJChangeBankView.h"
#import "RTJUserInfoCommntCell.h"
#import "RTJBindPayCardController.h"

@interface RTJChangeBankView ()<UITableViewDelegate,UITableViewDataSource>

@property (strong, nonatomic) UIView *bankView;
@property (strong, nonatomic)  UITableView *tableView;
@property (nonatomic,assign) BOOL isFirst;
@property (nonatomic,assign) NSIndexPath *index;

@end



@implementation RTJChangeBankView

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        [[NSBundle mainBundle] loadNibNamed:[[self class] description] owner:self options:nil];
        
        UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapGestures)];
        [self addGestureRecognizer:tapGesture];
        self.userInteractionEnabled = YES;
        
        self.backgroundColor = [UIColor colorForHex:@"#000000"];
        self.alpha = 0.5;
        [[UIApplication sharedApplication].keyWindow addSubview:self];
        [[UIApplication sharedApplication].keyWindow addSubview:_bankView];
        self.bankView = [[UIView alloc]initWithFrame:CGRectMake(0, screenH+257, screenW, 257)];
        self.bankView.backgroundColor = [UIColor whiteColor];
        self.bankView.alpha = 0.0;
        
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        btn.frame = CGRectMake(15, 15, 30,22);
        [btn setImage:[UIImage imageNamed:@"icon_yhcha"] forState:UIControlStateNormal];
        [btn addTarget:self action:@selector(cancel) forControlEvents:UIControlEventTouchUpInside];
        [self.bankView addSubview:btn];
        
        UILabel *lab = [[UILabel alloc]initWithFrame:CGRectMake(screenW/2-100, 15, 200, 22)];
        lab.text = _isTiXian ? @"选择提现银行卡": @"选择充值银行卡";
        lab.textAlignment = NSTextAlignmentCenter;
        lab.textColor = [UIColor colorForHex:@"##333333"];
        lab.font = [UIFont fontWithName:@"PingFang-SC-Bold" size:15];
        [self.bankView addSubview:lab];
        
        UIView *vi = [[UIView alloc]initWithFrame:CGRectMake(15, 15+22+15, screenW-30, 1)];
        vi.backgroundColor = [UIColor colorForHex:@"#e5e5e5"];
        [self.bankView addSubview:vi];
        
        
        self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 15+22+15+1, screenW, 257-(15+22+15+1)) style:UITableViewStylePlain];
        self.tableView.delegate = self;
        self.tableView.dataSource = self;
        self.tableView.tableFooterView=[UIView new];
        self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        [self.bankView addSubview:self.tableView];
        
        self.isFirst = YES;
        

    

    }
    return self;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return _isTiXian ? _data.count : _data.count+1;
    
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return 70;
    
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString * ID = @"RTJUserInfoCommntCell";
    RTJUserInfoCommntCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    
    if (_isTiXian) {

            if (!cell) {
                
                cell = [[NSBundle mainBundle] loadNibNamed:@"RTJUserInfoCommntCell" owner:self options:nil][8];
                
            }
            
            if (_isFirst) {
                
                if (indexPath.row == 0) {
                    
                    cell.selectImg.image = [UIImage imageNamed:@"icon_yhgou"];
                    
                }else{
                    
                    cell.selectImg.image = [UIImage imageNamed:@""];
                    
                }
            }else{
                
                cell.selectImg.image = _index == indexPath ? [UIImage imageNamed:@"icon_yhgou"]:[UIImage imageNamed:@""];
                
            }
            
            NSString *bankStr = [NSString stringWithFormat:@"%@",_data[indexPath.row][@"bankcard"]];
            bankStr = [bankStr substringFromIndex:bankStr.length-4];
            cell.selectBank.text = [NSString stringWithFormat:@"%@(尾号%@)",_data[indexPath.row][@"name"],bankStr];
    
        
    }else{
       
        if (indexPath.row == _data.count) {
            
            if (!cell) {
                
                cell = [[NSBundle mainBundle] loadNibNamed:@"RTJUserInfoCommntCell" owner:self options:nil][8];
                
            }
            
            cell.selectBank.text = @"添加银行卡快钱充值";
            cell.selectImg.image = [UIImage imageNamed:@"icon_daobiao"];
            
        }else{
            
            if (!cell) {
                
                cell = [[NSBundle mainBundle] loadNibNamed:@"RTJUserInfoCommntCell" owner:self options:nil][8];
                
            }
            
            if (_isFirst) {
                
                if (indexPath.row == 0) {
                    
                    cell.selectImg.image = [UIImage imageNamed:@"icon_yhgou"];
                    
                }else{
                    
                    cell.selectImg.image = [UIImage imageNamed:@""];
                    
                }
            }else{
                
                cell.selectImg.image = _index == indexPath ? [UIImage imageNamed:@"icon_yhgou"]:[UIImage imageNamed:@""];
                
            }
            
            NSString *bankStr = [NSString stringWithFormat:@"%@",_data[indexPath.row][@"bankcard"]];
            bankStr = [bankStr substringFromIndex:bankStr.length-4];
            cell.selectBank.text = [NSString stringWithFormat:@"%@(尾号%@)",_data[indexPath.row][@"name"],bankStr];
        }
        
        
    }

    cell.selectionStyle=UITableViewCellSelectionStyleNone;
    return  cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    _isFirst = NO;
    _index = indexPath;
    
    [self.tableView reloadData];
    
    [self  removeOperateView];
    
    

    if (indexPath.row == _data.count) {

        RTJBindPayCardController *vc = [[RTJBindPayCardController alloc]init];
        vc.bankListArr = _data;
        [_VC.navigationController pushViewController:vc animated:YES];

    }else{
        
        if (self.bankClick) {
            
            self.bankClick(self.data[indexPath.row]);
        }
    }
    
}


-(void)withSureClick:(bankBlock)block{
    
    _bankClick = block;

}

-(void)tapGestures{
    
    [UIView animateWithDuration:0.25 animations:^{
        self.alpha = 0.0;
        self.bankView.alpha = 0.0;
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
        [self.bankView removeFromSuperview];
    }];
}

- (void)cancel{
    
    [UIView animateWithDuration:0.25 animations:^{
        self.alpha = 0.0;
        self.bankView.alpha = 0.0;
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
        [self.bankView removeFromSuperview];
    }];
    
}

-(void)viewShow{
    //
    [[UIApplication sharedApplication].keyWindow addSubview:self];
    [[UIApplication sharedApplication].keyWindow addSubview:self.bankView];
    
    [UIView animateWithDuration:0.25 animations:^{
        self.alpha = 0.5;
        self.bankView.alpha = 1;
        self.bankView.frame  = CGRectMake(0, screenH-257, screenW, 257);

        [self.tableView reloadData];
        
    } completion:^(BOOL finished) {
        
    }];
    
}

-(void)removeOperateView{
    
    [UIView animateWithDuration:0.25 animations:^{
        self.alpha = 0.0;
        self.bankView.alpha = 0.0;
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
        [self.bankView removeFromSuperview];
    }];
}

@end
